package com.haoxi.gateway.test.message;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.haoxi.gateway.common.TestConfConst;
import com.haoxi.gateway.test.SGIPConstant;
import com.haoxi.gateway.test.util.TypeConvert;

public class SGIPBindMessage extends SGIPMessage {
    private static SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss");
    public SGIPBindMessage(byte buf[]) throws IllegalArgumentException {
        super.buf = new byte[61];
        if (buf.length != 61) {
            throw new IllegalArgumentException(SGIPConstant.SMC_MESSAGE_ERROR);
        } else {
            System.arraycopy(buf, 0, super.buf, 0, 61);
            // 消息头
            message_Length = TypeConvert.byte2int(super.buf, 0);
            command_ID = TypeConvert.byte2int(super.buf, 4);
            src_node_Id = TypeConvert.byte2int(super.buf, 8);
            time_Stamp = TypeConvert.byte2int(super.buf, 12);
            sequence_Id = TypeConvert.byte2int(super.buf, 16);
        }
    }

    public SGIPBindMessage(int login_Type, String login_Name,
            String login_Password) throws IllegalArgumentException {
        if (login_Name == null)
            throw new IllegalArgumentException(String.valueOf(String
                    .valueOf((new StringBuffer(String.valueOf(String
                            .valueOf(SGIPConstant.CONNECT_INPUT_ERROR))))
                            .append(":login_Name").append(
                                    SGIPConstant.STRING_NULL))));
        if (login_Name.length() > 16)
            throw new IllegalArgumentException(String.valueOf(String
                    .valueOf((new StringBuffer(String.valueOf(String
                            .valueOf(SGIPConstant.CONNECT_INPUT_ERROR))))
                            .append(":login_Name").append(
                                    SGIPConstant.STRING_LENGTH_GREAT).append(
                                    "16"))));
        if (login_Password == null)
            throw new IllegalArgumentException(String.valueOf(String
                    .valueOf((new StringBuffer(String.valueOf(String
                            .valueOf(SGIPConstant.CONNECT_INPUT_ERROR))))
                            .append(":login_Password").append(
                                    SGIPConstant.STRING_NULL))));
        if (login_Password.length() > 16)
            throw new IllegalArgumentException(String.valueOf(String
                    .valueOf((new StringBuffer(String.valueOf(String
                            .valueOf(SGIPConstant.CONNECT_INPUT_ERROR))))
                            .append(":login_Password").append(
                                    SGIPConstant.STRING_LENGTH_GREAT).append(
                                    "16"))));
        if (login_Type < 0 || login_Type > 255) {
            throw new IllegalArgumentException(String.valueOf(String
                    .valueOf((new StringBuffer(String.valueOf(String
                            .valueOf(SGIPConstant.CONNECT_INPUT_ERROR))))
                            .append(":login_Type").append(
                                    SGIPConstant.INT_SCOPE_ERROR))));
        } else {
            int len = 61;
            buf = new byte[len];
            TypeConvert.int2byte(len, buf, 0);
            TypeConvert.int2byte(1, buf, 4);
            int icpid = TestConfConst.icpid & 0xffffffff;
            TypeConvert.int2byte(icpid, buf, 8);
            int timeStamp=Integer.parseInt(format.format(new Date())); 
            TypeConvert.int2byte(timeStamp, buf, 12);
            setSequenceID();
            buf[20] = (byte) login_Type;
            System.arraycopy(login_Name.getBytes(), 0, buf, 21, login_Name
                    .length());
            System.arraycopy(login_Password.getBytes(), 0, buf, 37,
                    login_Password.length());
        }
    }

    public int getTime_Stamp() {
        time_Stamp = TypeConvert.byte2int(super.buf, 12);
        return time_Stamp;
    }

    public int getSequence_Id() {
        sequence_Id = TypeConvert.byte2int(super.buf, 16);
        return sequence_Id;
    }

    public int getMsgLength() {
        message_Length = TypeConvert.byte2int(super.buf, 0);
        return message_Length;
    }

    public int getSrcNodeId() {
        src_node_Id = TypeConvert.byte2int(super.buf, 8);
        return src_node_Id;
    }

    public int getLoginType() {
        int tmpId = buf[20];
        return tmpId;
    }

    public String getLoginName() {
        byte tmpId[] = new byte[16];
        System.arraycopy(buf, 21, tmpId, 0, 16);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0)
            return tmpStr.substring(0, tmpStr.indexOf(0));
        else
            return tmpStr;
    }

    public String getLoginPass() {
        byte tmpId[] = new byte[16];
        System.arraycopy(buf, 37, tmpId, 0, 16);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0)
            return tmpStr.substring(0, tmpStr.indexOf(0));
        else
            return tmpStr;
    }

    public String getReserve() {
        byte tmpId[] = new byte[8];
        System.arraycopy(buf, 53, tmpId, 0, 8);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0)
            return tmpStr.substring(0, tmpStr.indexOf(0));
        else
            return tmpStr;
    }

    public String toString() {
        String tmpStr = "SGIP_BIND: ";
        StringBuffer buff = new StringBuffer(tmpStr);
        buff.append("message_Length=").append(this.getMsgLength()).append(
                "\r\n").append("CommandId=").append(this.getCommandId())
                .append("\r\n").append("src_node_Id=").append(
                        this.getSrcNodeId()).append("\r\n").append(
                        "time_Stamp:").append(this.getTime_Stamp()).append(
                        "\r\n").append("sequence_Id=").append(
                        this.getSequence_Id()).append("\r\n").append(
                        "LoginType:").append(this.getLoginType())
                .append("\r\n").append("LoginName=")
                .append(this.getLoginName()).append("\r\n")
                .append("LoginPass=").append(this.getLoginPass())
                .append("\r\n").append("Reserve=").append(this.getReserve())
                .append("\r\n");
        return buff.toString();
    }

    public int getCommandId() {
        command_ID = TypeConvert.byte2int(super.buf, 4);
        return command_ID;
    }
}
